// 专门爬取数据的模块

import axios from 'axios'
import * as utils from './utils'

axios.defaults.timeout = 30000
const lichessPath = 'https://explorer.lichess.ovh/masters'

interface ILichessResponse {
  white: number,
  draws: number,
  black: number,
  moves: {
    uci: string,
    san: string,
    "averageRating": number,
    "white": number,
    "draws": number,
    "black": number,
    "game": any
  }[],
  topGames: {
    "uci": string,
    "id": string,
    "winner": string,
    "black": {
      "name": string,
      "rating": number
    },
    "white": {
      "name": string,
      "rating": number
    },
    "year": number,
    "month": string // 2019-07
  }[],
  opening: {
    eco: string,
    name: string
  }
}

// 根据fen和走法获取局面名称
export const getFenNameEN = async (fen: string, move: string): Promise<{nameEn: string, eco: string}> => {
  try {
    const res = await axios.get(lichessPath, { params:{fen, play: move} } )
    const data = res.data as unknown as ILichessResponse
    let text = ''
    let eco = ''
    if (data.opening?.eco) {
      text = data.opening.eco + ' ' + data.opening.name
      eco = data.opening.eco
    }
    return {nameEn: text, eco: eco}
  } catch(err) {
    console.log("获取局面名称出错")
    return {nameEn: '', eco: ''}
  }

}

export const getBigData = async (fen: string): Promise<any> => {
  if (!fen || fen === '') fen = utils.newGame
  try {
    const res = await axios.get(lichessPath, { params:{fen} } )
    const data = res.data as unknown as ILichessResponse
    const bigData = []
    const topGames = []
    if (data.moves) {
      for (const move of data.moves) {
        const total = move.white + move.draws + move.black
        const white = move.white
        const black = move.black
        const draw = move.draws
        const result = `${white}/${draw}/${black}`
        bigData.push({move: move.san, total, white, black, draw, result })
      }
    }
    if (data.topGames) {
      for (const game of data.topGames) {
        let result = null
        if (game.winner === null) result = '1/2-1/2'
        if (game.winner === 'black') result = '0-1'
        if (game.winner === 'white') result = '1-0'
        topGames.push({white: game.white.name, black: game.black.name, date: game.month, result: result})
      }
    }
    let text = ''
    let eco = ''
    if (data.opening?.eco) {
      text = data.opening.eco + ' ' + data.opening.name
      eco = data.opening.eco
    }
    return {bigData, topGames, eco, nameEn: text}
  } catch(err) {
    console.log("获取大数据出错")
    return {topGames: [], bigData: []}
  }
}

export const getMyGames = async (userName: string, since: string | number = null): Promise<any> => {
  let path = `https://lichess.org/api/games/user/${userName}?tags=true&clocks=false&evals=false&opening=true`
  if (since) path += `&since=${since}`
  try {
    const res = await axios.get(path, {params: {}, timeout: 10000000000000000000000})
    const data = res.data
    return {data}
  } catch (err) {
    if (err.response?.status === 404) {return {status: 404}}
    else {return {status: 500}}
  }
}
